﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.IdentityModel.Claims;
using Facebook;
using System.Configuration;

namespace LogOnWithACSLibrary
{
    /// <summary>
    /// This is a custom claims authentication manager which processes the claims returned from ACS.
    /// It uses FacebookAccessToken to get the user's facebook profile.
    /// </summary>
    public class CustomClaimsAuthenticationManager : ClaimsAuthenticationManager
    {
        public override IClaimsPrincipal Authenticate(string resourceName, IClaimsPrincipal incomingPrincipal)
        {
            if (!incomingPrincipal.Identity.IsAuthenticated)
            {
                return base.Authenticate(resourceName, incomingPrincipal);
            }

            IClaimsIdentity claimsIdentity = incomingPrincipal.Identity as IClaimsIdentity;

            string fbAccessToken = (from c in claimsIdentity.Claims
                                    where c.ClaimType == FacebookClaimTypes.FacebookAccessToken
                                    select c.Value).FirstOrDefault();
            if (fbAccessToken != null)
            {
                GetFacebookClaims(fbAccessToken, incomingPrincipal);
            }

            return incomingPrincipal;
        }

        private void GetFacebookClaims(string facebookAccessToken, IClaimsPrincipal incomingPrincipal)
        {

            var fb = new FacebookClient(facebookAccessToken);
            dynamic me = fb.Get(ConfigurationManager.AppSettings["FacebookApplicationPermissions"]);

            try
            {
                if (!string.IsNullOrEmpty(me.id))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookUserId, me.id));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.first_name))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookFirstName, me.first_name));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.last_name))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookLastName, me.last_name));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.link))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookLink, me.link));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.username))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookUserName, me.username));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.birthday))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookBirthday, me.birthday));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.hometown.name))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookHomeTown, me.hometown.name));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.location.name))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookLocation, me.location.name));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.gender))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookGender, me.gender));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.timezone.ToString()))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookTimeZone, me.timezone.ToString()));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.locale))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookLocale, me.locale));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.verified.ToString()))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookVerified, me.verified.ToString()));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.updated_time))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookUpdatedTime, me.updated_time));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.bio))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookAboutMe, me.bio));
                }
            }
            catch { }

            try
            {
                if (!string.IsNullOrEmpty(me.picture))
                {
                    incomingPrincipal.Identities[0].Claims.Add(new Claim(FacebookClaimTypes.FacebookProfilePicture, me.picture));
                }
            }
            catch { }
        }

    }
}
